/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.harvest.tei.svn;

import cz.mzk.oai.metadataRepository.harvest.tei.svnManager.Connector;
import cz.mzk.oai.metadataRepository.metadata.MetadataFormat;
import cz.mzk.oai.metadataRepository.repository.Identifier;
import cz.mzk.oai.metadataRepository.repository.Repository;
import cz.mzk.oai.metadataRepository.repository.collections.NoCollectionFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.tmatesoft.svn.core.SVNException;

/**
 *
 * @author Martin Řehánek (rehan at mzk.cz)
 */
public class RecordHarvester {

    private static final Logger logger = Logger.getLogger(RecordHarvester.class.getName());
    private static final String FILE_SUFFIX = ".tei.xml";
    private final SignatureSysnoMapper mapper;
    private final Connector svnConnector;
    private final Repository repository;

    RecordHarvester(Repository repository, Connector svnManager, SignatureSysnoMapper mapper) {
        this.repository = repository;
        this.svnConnector = svnManager;
        this.mapper = mapper;
    }

    void harvestRecord(String filename, MetadataDir metadataDir, long revision) {
        logger.log(Level.INFO, "harvesting file {0}", filename);
        String signature = signatureFromFilename(filename);
        if (signature == null) {
            logger.log(Level.SEVERE, "No base,sysno found for signature {0}", signature);
        } else {
            Identifier id = mapper.getIdentifier(signature);
            if (id == null) {
                logger.log(Level.WARNING, "no identifier found for signature {0}", signature);
            } else {
                logger.log(Level.INFO, "signature: {0}, id: {1}", new Object[]{signature, id});
                try {
                    Document teiDoc = svnConnector.getXmlContent(metadataDir, filename, revision);
                    addOrUpdateTeiRecord(id, teiDoc);
                } catch (SVNException ex) {
                    Logger.getLogger(TeiSvnHarvester.class.getName()).log(Level.SEVERE, null, ex);
                } catch (DocumentException ex) {
                    Logger.getLogger(TeiSvnHarvester.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    logger.log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    private String signatureFromFilename(String file) {
        return file.substring(0, file.length() - FILE_SUFFIX.length());
    }

    private void addOrUpdateTeiRecord(Identifier id, Document data) throws IOException {
        try {
            repository.updateRecord(id, MetadataFormat.tei_p5, data);
        } catch (NoCollectionFoundException ex) {
            Logger.getLogger(RecordHarvester.class.getName()).log(Level.SEVERE, id.toString(), ex);
            //TODO: vybrat kolekci pro novou polozku, vlozit. Jako je to u MarcHarvester
        }
    }
}
